<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 14:52
 */

namespace app\api\controller;


use app\common\model\Config;
use app\common\model\MallConfig;
use app\common\model\Mall;
use app\model\UserModel;
use app\service\LogService;
use EasyWeChat\Factory;
use think\Controller;

header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET,POST");
header("Access-Control-Allow-Headers:x-requested-with,content-type");
class AllBase extends Controller
{
    public $user;//用户信息
    public $mall;//子商城信息
    public $userModel;
    public $mallModel;
    public $openPlatform;//开发平台授权管理
    public $officialAccount;
    public $mall_id;

    public function __construct()
    {
        LogService::S()->writeLog('st','ts',1);
        parent::__construct();
        $this->userModel = new UserModel();
        $this->mallModel = new Mall();
        $this->setConfig();

       /*  $config = [
            'app_id'   => config('open_appid'),
            'secret'   => config('open_secret'),
            'token'    => config('open_token'),
            'aes_key'  => config('open_aeskey'),
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/api/login/wechat_callback',
            ]
        ];

        $this->openPlatform=Factory::openPlatform($config); */

        $config = [
            'app_id' => config('wechat_appid'),
            'secret' => config('wechat_appsecret'),
            'response_type' => 'array',
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/api/login/wechat_callback',
            ]
        ];
        $this->officialAccount = Factory::officialAccount($config);

        $this->checkMall();
       /*  config('authorizer_appid',$this->mall['authorizer_appid']); //设置配置授权Appid
        config('authorizer_refresh_token',$this->mall['authorizer_refresh_token']);
        $this->officialAccount = $this->openPlatform->officialAccount(config('authorizer_appid'), config('authorizer_refresh_token')); */


        config('mall_id',$this->mall_id);
        config(MallConfig::getConfig($this->mall_id));
        /*$this->setConfig();
        $this->debugLogin();*/
        if(request()->isAjax()) {
            if(config('site_open')==0 || config('shop_open')==0) {
                $this->ajax(204,'',request()->domain().'/index/index/close?mall_id='.$this->mall_id);
            }
        }
    }



    /* 全局配置 */
    protected function setConfig () {
        $configModel = new Config();
        config($configModel->getConfig());
    }


    /* 调试模式 */
    public function debugLogin () {

    }

    public function checkMall () {
        $input = input();

        if($input['mall_id']) {
            $mall = $this->mallModel->getOne([
                'id' => $input['mall_id']
            ]);
        }elseif(session('mall')){
            $mall = session('mall');
            $mall = $this->mallModel->getOne([
                'id' => $mall['id']
            ]);
        }
        LogService::S()->writeLog('all','data',$input);
        if(!$mall) {
            $this->error('error');exit;
        }
        $this->mall = $mall;
        $this->mall_id=$mall['id'];
        session('mall',$mall);
    }

     /**
     * 返回
     * @param $code
     * @param $message
     * @param null $data
     * @param null $dataName
     */
    protected function ajax($code,$message,$data=null,$dataName=null){
        if($data!=null && $data!==0){
            if($dataName==null){
               echo json_encode(array('code'=>$code,'msg'=>$message,'data'=>$data));
                exit();
            }else{
               echo json_encode(array('code'=>$code,'msg'=>$message,$dataName=>$data));
                exit();
            }
        }else{
           echo json_encode(array('code'=>$code,'msg'=>$message,'data'=>$data)) ;
            exit();
        }
    }


}